package com.qfjy.project.meeting;
import cn.hutool.poi.word.WordUtil;
import com.qfjy.entity.Publock;
import com.qfjy.entity.User;
import com.qfjy.service.PublockService;
import com.qfjy.service.UserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
@Slf4j
@Controller
@RequestMapping("user")
@Api(tags = {"用户的各项操作"})
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private PublockService publockService;
    /**
     * 个人中心
     */
    @ResponseBody
    @RequestMapping("login")
    @ApiOperation(value = "用户的登录",notes = "根据用email查询用户是否存在")
   @ApiImplicitParams({
           @ApiImplicitParam(name = "uid", value = "用户id", required = true,defaultValue = "-1"),
           @ApiImplicitParam(name = "wid", value = "微信的id", required = true,defaultValue = "-1")
   })
    public String login(@RequestParam("email") final String email, @RequestParam("wid") final int wid, HttpServletRequest request) {
        User user = userService.selectUserByEmail(email);
        if (user == null) {
            return "1";//邮箱不存在,联系管理员
        } else {
            if (user.getWid() != null) {
                return "2";//该邮箱已被登录,请联系管理员
            } else {
                int i = userService.updateUserWidByEmail(wid, email);
                log.info("用户绑定成功");
                return "3";
            }
        }
    }
    @ResponseBody
    @PostMapping("update")
    public int update(User user) {
        int i = userService.updateByPrimaryKeySelective(user);
        log.info("用户个人信息更新成功");
        return i;
    }

    /**
     * 进入登录页面
     */
   @RequestMapping("toLoginPage")
    public String toLoginPage(HttpServletRequest request) {
        String wid = request.getParameter("wid");
        request.setAttribute("wid", wid);
        return "pages/user/login";
    }

    /**
     * 无权页面
     */
    @RequestMapping("toUnAuthPage")
    public String toUnAuthPage(){
        return "pages/unauth";
    }
    /**
     * 进入抢单者列表
     *
     */
    @RequestMapping("toMeetingGrabPage")
    public String toMeetingGrabPage(HttpServletRequest request){
        String  uid = (String) request.getParameter("uid");
        request.setAttribute("uid",uid);
        return "pages/meetingGrab/meetingGrab";
    }
    @RequestMapping("dailySign")
    public String dailySign(String uid,HttpServletRequest request){
        System.out.println("222222222222222222"+uid);
        request.setAttribute("uid",uid);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Publock publock = new Publock();
        Random random = new Random();
        publock.setSignData(sdf.format(new Date()));
        publock.setId(random.nextInt());
        publock.setUid(Integer.parseInt(uid));
        int i = publockService.insertSelective(publock);
        if(i==0){
            request.setAttribute("mes","今日已签到");
        }
        List<Publock> publocks = publockService.selectSignList(uid);
        int  sign_score=0;
        String str="";
        for (Publock p :
                publocks) {
            str+=p.getSignData()+",";
            sign_score+=p.getSignScore();
        }
        request.setAttribute("score",sign_score);
        request.setAttribute("str",str);
      return "pages/user/dailySign";
    }
}